package com.relife.core.easyui;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;


import com.relife.common.model.Client;
import com.relife.common.model.ClientManager;

public class JsCssTag extends TagSupport {

	private static final long serialVersionUID = -4134622892594943541L;
	private static final String easyui = "easyui";
	private static final String ztree = "ztree";
	private static final String validate = "validate";
	private static final String lhgdialog = "lhgdialog";
	private static final String DatePicker = "datePicker";
	private static final String home = "home";
	private static final String upload = "upload";
	private static final String kindeditor = "kindeditor";
	private static final String highcharts = "highcharts";
	private static final String jqueryQr="jqueryQr";
	private String type;

	@Override
	public int doStartTag() throws JspTagException {
		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			JspWriter out = this.pageContext.getOut();
			out.print(this.getJsCss());
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	/**
	 * 
	 * @author WU
	 * @date 2016年5月27日 下午4:27:48
	 * @return
	 */
	private StringBuffer getJsCss() {
		StringBuffer css = new StringBuffer();
		StringBuffer js = new StringBuffer();
		Client client = ClientManager.getInstance().getClientSession();
		String base = this.pageContext.getRequest().getServletContext().getContextPath();
		css.append("<link href=\"" + base + "/css/base.css\" rel=\"stylesheet\" type=\"text/css\" />\n");

		if (type.contains(easyui)) {
			css.append("<link href=\"" + base + "/common/jquery-easyui-1.4.2/themes/" + client.getTheme() + "/easyui.css\" rel=\"stylesheet\" type=\"text/css\"/>\n");
			css.append("<link href=\"" + base + "/common/jquery-easyui-1.4.2/themes/icon.css\" rel=\"stylesheet\" type=\"text/css\" />\n");

			js.append("<script type=\"text/javascript\" src=\"" + base + "/common/jquery.min.js\"></script>\n");
			js.append("<script type=\"text/javascript\" src=\"" + base + "/common/jquery-easyui-1.4.2/jquery.easyui.min.js\"></script>\n");
		}

		js.append("<script type=\"text/javascript\" src=\"" + base + "/common/common.js\"></script>\n");

		if (type.contains(home)) {
			css.append("<link href=\"" + base + "/css/home.css\" rel=\"stylesheet\" type=\"text/css\"/>\n");
			js.append("<script type=\"text/javascript\" src=\"" + base + "/common/home.js\"></script>\n");
		}

		if (type.contains(validate)) {
			js.append("<script type=\"text/javascript\" src=\"" + base + "/common/jquery.validationEngine.js\"></script>\n");
		}

		if (type.contains(lhgdialog)) {
			js.append("<script type=\"text/javascript\" src=\"" + base + "/common/lhgdialog/lhgdialog.min.js\"></script>\n");
		}

		if (type.contains(ztree)) {
			css.append("<link href=\"" + base + "/common/zTree/css/zTreeStyle.css\" rel=\"stylesheet\" type=\"text/css\"/>\n");

			js.append("<script type=\"text/javascript\" src=\"" + base + "/common/zTree/js/jquery.ztree.all.min.js\"></script>\n");
		}

		if (type.contains(DatePicker)) {
			js.append("<script type=\"text/javascript\" src=\"" + base + "/common/My97DatePicker/WdatePicker.js\"></script>\n");
		}

		if (type.contains(upload)) {
			js.append("<script type=\"text/javascript\" src=\"" + base + "/common/uploadify/jquery.uploadify.min.js\"></script>\n");
		}
		if (type.contains(kindeditor)) {
			js.append("<script charset=\"utf-8\" src=\"" + base + "/common/kindeditor-4.1.10/kindeditor.js\"></script>\n");
			js.append("<script charset=\"utf-8\" src=\"" + base + "/common/kindeditor-4.1.10/lang/zh_CN.js\"></script>\n");
		}
		if(type.contains(highcharts)){
			js.append("<script type=\"text/javascript\" src=\"" + base + "/common/Highcharts/highcharts.js\"></script>\n");
			//js.append("<script type=\"text/javascript\" src=\"" + base + "/common/Highcharts/exporting.js\"></script>\n");
		}
		if(type.contains(jqueryQr)){
			js.append("<script charset=\"utf-8\" src=\"" + base + "/common/jqueryqr.js\"></script>\n");
		}
		
		StringBuffer sb = new StringBuffer();
		sb.append(css);
		sb.append(js);
		return sb;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public static void main(String[] args) {
		System.out.println("sdfe".contains("sdf"));
	}
}
